// 导演类，控制游戏的逻辑
import { DataStore } from "./base/dataStore.js";
import { UpPencil } from "./runtime/upPencil.js";
import { DownPencil } from "./runtime/downPencil.js";
export class Director{

  constructor(){
    this.dataStore = DataStore.getInstance();
    this.moveSpeed = 2;
    console.log("导演类构造器初始化")
  }
  static getInstance(){
    if(!Director.instance){
      Director.instance = new Director()
    }
    return Director.instance;
  }

  
  createPencil(){
    const minTop = DataStore.getInstance().canvas.height / 8;
    const maxTop = DataStore.getInstance().canvas.height / 2;
    const top = minTop + Math.random() * (maxTop-minTop);
    this.dataStore.get("pencils").push(new UpPencil(top))
    this.dataStore.get("pencils").push(new DownPencil(top))
  }

  birdsEvent() {
    for (let i = 0; i <= 2; i++) {
      this.dataStore.get('birds').y[i] =
        this.dataStore.get('birds').birdsY[i];
    }
    this.dataStore.get('birds').time = 0;
  }

  //判断小鸟是否和铅笔撞击
  static isStrike(bird, pencil) {
    let s = false;
    if (bird.top > pencil.bottom ||
      bird.bottom < pencil.top ||
      bird.right < pencil.left ||
      bird.left > pencil.right
    ) {
      s = true;
    }
    return !s;
  }

  //判断小鸟是否撞击地板和铅笔
  check() {
    const birds = this.dataStore.get('birds');
    const land = this.dataStore.get('land');
    const pencils = this.dataStore.get('pencils');
    const score = this.dataStore.get('score');

    //地板的撞击判断
    if (birds.birdsY[0] + birds.birdsHeight[0] >= land.y) {
      console.log('撞击地板啦');
      this.isGameOver = true;
      return;
    }

    //小鸟的边框模型
    const birdsBorder = {
      top: birds.y[0],
      bottom: birds.birdsY[0] + birds.birdsHeight[0],
      left: birds.birdsX[0],
      right: birds.birdsX[0] + birds.birdsWidth[0]
    };

    const length = pencils.length;
    for (let i = 0; i < length; i++) {
      const pencil = pencils[i];
      const pencilBorder = {
        top: pencil.y,
        bottom: pencil.y + pencil.height,
        left: pencil.x,
        right: pencil.x + pencil.width
      };

      if (Director.isStrike(birdsBorder, pencilBorder)) {
        // console.log(birdsBorder)
        // console.log(pencilBorder)
        console.log('撞到水管啦');
        this.isGameOver = true;
        return;
      }
    }

    //加分逻辑
    if (birds.birdsX[0] > pencils[0].x + pencils[0].width
      && score.isScore) {
      // wx.vibrateShort({
      //   success: function () {
      //     console.log('振动成功');
      //   }
      // });
      score.isScore = false;
      score.scoreNumber++;
    }
  }

  run(){
    this.check();
    if (this.isGameOver) {
      // 这里如果不绘制个底图，则在安卓中会出现花屏现象，
      // 很奇怪，无法解决
      this.dataStore.get('background').draw();
      this.dataStore.get('score').draw();
      this.dataStore.get("pencils").forEach(function (value) {
        value.draw();
      })
      this.dataStore.get('land').draw();
      this.dataStore.get('startButton').draw();
      cancelAnimationFrame(this.dataStore.get('timer'));

      this.dataStore.destory();
      //触发微信小游戏垃圾回收
      // wx.triggerGC();
    }else if (!this.isGameHasStart) {
      this.dataStore.get('background').draw();
      this.dataStore.get('land').draw();
      this.dataStore.get('score').draw();
      this.dataStore.get('birds').draw();
      this.dataStore.get('startButton').draw();
    } else{
      this.dataStore.get('background').draw();

      const pencils = this.dataStore.get('pencils');

      // 当最左边的铅笔 消失了 && 当前铅笔总数为4，则去除最左边两个铅笔
      if (pencils[0].x + pencils[0].width <= 0 &&
        pencils.length === 4) {
        pencils.shift();
        pencils.shift();
        this.dataStore.get('score').isScore = true;
      }
      // 当最左边的铅笔 刚好移动到中间位置 则开始生成第二组铅笔
      if (pencils[0].x <= (DataStore.getInstance().canvas.width - pencils[0].width) / 2 &&
        pencils.length === 2) {
        this.createPencil();
      }
      this.dataStore.get("pencils").forEach(function (value) {
        value.draw();
      })
      this.dataStore.get('land').draw();
      this.dataStore.get('score').draw();
      this.dataStore.get('birds').draw();
      // 类似于setTimeOut
      let timer = requestAnimationFrame(() => {
        this.run();
      })
      this.dataStore.put("timer", timer)
    }
  }
}